﻿using System;
using System.Reflection;
using MySql.Data.MySqlClient;
using MySql.Simple.Mapper.Attributes;
using MySql.Simple.Mapper.Structures;

namespace MySql.Simple.Mapper.Types
{
    /// <summary>
    /// Class to handle the mapping of <see cref="String"/> types to <see cref="Column"/> types.
    /// </summary>
    public class Int16Link : ITypeLink
    {
        /// <summary>
        /// Determines whether this link is responsible for mapping the specified type.
        /// </summary>
        /// <param name="propertyInfo">The property info.</param>
        /// <returns>
        /// 	<c>true</c> if the specified type is responsible; otherwise, <c>false</c>.
        /// </returns>
        public bool IsResponsible(PropertyInfo propertyInfo)
        {
            return propertyInfo.PropertyType == typeof (Int16);
        }

        /// <summary>
        /// Maps the specified column.
        /// </summary>
        /// <param name="column">The column.</param>
        /// <param name="columnAttribute">The column attribute.</param>
        /// <returns></returns>
        public Column Map(Column column, ColumnAttribute columnAttribute)
        {
            // Default string type to VarChar
            if (!columnAttribute.TypeHasValue)
            {
                column.Type = MySqlDbType.Int16;    
            }

            // Default length to 6
            if (column.Length == 0)
            {
                column.Length = 6;
            }

            return column;
        }
    }
}
